package File;
/**
*This Class was Supposed to Deal with file makeing them and saving them on the hard drive 
*of client \ server or both if it was necessary
*/
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.Serializable;

public class CMyFile implements Serializable {
	
	private String Description=null;
	private String fileName=null;	
	private int size=0;
	public  byte[] mybytearray;
	
	
	public void initArray(int size)
	{
		mybytearray = new byte [size];	
	}
	
	public CMyFile( String fileName) {
		this.fileName = fileName;
	}
	
	
	public String getFileName() {
		return fileName;
	}

	public void setFileName(String fileName) {
		this.fileName = fileName;
	}
	
	public int getSize() {
		return size;
	}

	public void setSize(int size) {
		this.size = size;
	}

	public byte[] getMybytearray() {
		return mybytearray;
	}
	
	public byte getMybytearray(int i) {
		return mybytearray[i];
	}

	public void setMybytearray(byte[] mybytearray) {
		
		for(int i=0;i<mybytearray.length;i++)
		this.mybytearray[i] = mybytearray[i];
	}

	public String getDescription() {
		return Description;
	}

	public void setDescription(String description) {
		Description = description;
	}	
	
////////////////////////////////////////////////////////////////////////////////////////
//GGG

	  public CMyFile sendFileFromClientToServer(String name,String filePath){
		  try{
		  CMyFile msg= new CMyFile(name);
		  String LocalfilePath=filePath;
	  	  System.out.println("aaaaa"); 
		  File newFile = new File (LocalfilePath);
		  System.out.println("bbbbbbb");      		      
	      byte [] mybytearray  = new byte [(int)newFile.length()];
			      System.out.println("cccc"); 
			      FileInputStream fis = new FileInputStream(newFile);
			      BufferedInputStream bis = new BufferedInputStream(fis);			  
			      System.out.println("dddddddddddd"); 
			      msg.initArray(mybytearray.length);
			      msg.setSize(mybytearray.length);
			      System.out.println("saaaaa"); 
			      bis.read(msg.getMybytearray(),0,mybytearray.length);
			  
			      System.out.println("bassasas"+msg.getFileName()); 
			      return(msg);
		  }
		  catch (Exception e) {
				System.out.println("Error sendFileFromClientToServer");
			  
		  }
		return null;


}
	  public void saveFileOnServer(Object msg){
		  System.out.println("File received: " + msg );
		  
		  File newFile = new File ("d:\\S\\"+((CMyFile) msg).getFileName());
		  FileOutputStream fos;
		  try {

		  fos = new FileOutputStream(newFile);
		  BufferedOutputStream bos = new BufferedOutputStream(fos);		
          
		  bos.write(((CMyFile) msg).getMybytearray(),0,((CMyFile) msg).mybytearray.length);
		  bos.close();
		  fos.close();
		  System.out.println("Finishhhhhhhhhhh");
		  } catch (FileNotFoundException e) {
		  // TODO Auto-generated catch block
		  e.printStackTrace();
		  } catch (IOException e) {
		  // TODO Auto-generated catch block
		  e.printStackTrace();
		  }
	  }

}

